﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DbLinq;
using MySql.Data;
using MySql.Data.MySqlClient;


namespace LibFriesland_Campina_CRM
{
    class DalAccount
    {
        
        //Them mot Account moi
        public static int addAccount(BizAccount pAccount)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                Account account = new Account();
                account.UserName = pAccount.username;

                CEncrypt encrypt = new CEncrypt(); //Class ma hoa
                account.Password = encrypt.MaHoa(encrypt.encryptKey, pAccount.password);
                
                account.ID = pAccount.id;
                account.ByPrivilege = pAccount.by_privilege;
                account.Disabled = pAccount.disabled;

                db.Account.InsertOnSubmit(account);
                db.SubmitChanges();
                return 1;
            }
            catch(Exception ex)
            {
                return -1;
            }
        }

        //Xoa mot Account
        public static int deleteAccount(string pUsername)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                var account = db.Account.Single(p => p.UserName.Trim() == pUsername.Trim());
                db.Account.DeleteOnSubmit(account);
                db.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        #region Dung cho Webservice
        //Lay danh sach cac Account
        public static List<BizAccount> getAll()
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            var accounts = from acc in db.Account
                           select new BizAccount(acc.UserName, acc.Password, acc.ID, acc.ByPrivilege, Convert.ToBoolean(acc.Disabled));
            return accounts.ToList();
        }

        //Cap nhat thong tin Account
        public static int updateAccount(BizAccount pAccount)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                var account = db.Account.Single(p => p.UserName.Trim() == pAccount.username.Trim());
                CEncrypt encrypt = new CEncrypt(); //Class ma hoa
                account.Password = encrypt.MaHoa(encrypt.encryptKey, pAccount.password);
                account.ID = pAccount.id;
                account.ByPrivilege = pAccount.by_privilege;
                account.Disabled = pAccount.disabled;

                db.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        //Dang nhap
        public static BizAccount Login(string pUsername, string pPassword)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                CEncrypt encrypt = new CEncrypt(); //Class ma hoa
                var account = db.Account.Single(p => p.UserName == pUsername.Trim());
                var passwordDecode = encrypt.GiaiMa(encrypt.encryptKey, account.Password);
                if (pPassword == passwordDecode)
                {
                    //Doi voi account la NA, tim them shop NA lam viec
                    if (account.ByPrivilege == "3")
                    {
                        var na = db.NA.Single(p => p.NACharID == account.ID);
                        return new BizAccount(account.UserName, account.ID, account.ByPrivilege);
                    }
                    else
                    {
                        return new BizAccount(account.UserName, account.ID, account.ByPrivilege);
                    }
                }
                return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        //Doi mat khau
        public static int changePassword(string pUsername, string pOldPassWord, string pNewPassword)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                CEncrypt encrypt = new CEncrypt(); //Class ma hoa
                var account = db.Account.Single(p => p.UserName == pUsername.Trim());
                var passwordDecode = encrypt.GiaiMa(encrypt.encryptKey, account.Password);

                //Kiem tra password cu co hop le
                if (pOldPassWord == passwordDecode)
                {
                    account.Password = encrypt.MaHoa(encrypt.encryptKey, pNewPassword);
                    db.SubmitChanges();
                    return 1;
                }
                else
                {
                    return -1;
                }
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        #endregion
    }
}
